package com.org.cys.util;

import java.util.Map;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;


/**
 * SpringUtil 工具类
 * @author caiyushen
 * */
@Component
@Lazy(false)
public final class SpringUtils implements DisposableBean, ApplicationContextAware {
    private static ApplicationContext context;

    private SpringUtils() {
    }

   
    public void setApplicationContext(ApplicationContext applicationContext) {
        context = applicationContext;
    }

    /**
     * destroy 在接口中 org.springframework.beans.factory.DisposableBean
     * */
    public void destroy() {
        context = null;
    }

    /**
     * 获取applicationContext
     * */
    public static ApplicationContext getApplicationContext() {
        return context;
    }

    /**
     * 从Spring容器中获取指定名称的对象
     * @param T - 对象类型  -->  beanName - bean名称
     * @return spring容器中的对象
     * */
    public static <T> T getBean(String beanName) {
        Assert.hasText(beanName);
        return (T)context.getBean(beanName);
    }

    /**
     * 从spring容器中获取指定类型的对象键值列表
     * @param T - 类型
     * @param clazz - 类型
     * @return Map 类型键值列表
     * */
    public static <T> Map<String, T> getBeanByType(Class<T> clazz) {
        Assert.notNull(clazz);
        return context.getBeansOfType(clazz);
    }
}
